#include "test_performance.h"
#include "test_parser.h"
#include <sea_statemachine>

// -------------------------------------------------------------------------------------------------

int the_bin[ ] = 
#if 1
{
    0, 0, 0,            // Reserved

    2,                  // EXM :: SYNC
    14,                 // NOS
    1,                  // ISI
        // 0. Exception
        0, 0, 9,        // HBI HBN NOR
            4, 15, 13,  // EAS
            5, 15, 13,
            6, 15, 13,
            7, 15, 13,
            8, 15, 13,
            9, 15, 13,
            10, 15, 13,
            3, 15, 13,
            12, 15, -1,
        // 1. SmName
        0, 0, 8,
            -3,  0, -1,
            0,  2, -1,
            1, -1, 13,
            2, 15, 13,
            4,  2, -1,
            6,  3, -1,
            8,  4,  2,
            5, -1, 13,
        // 2. ExecMode
        0, 0, 5,
            -3,  3, -1,
            4,  2, -1,
            6,  3, -1,

            8,  5,  3,
            9, -1,  4,
        // 3. AtSmEntry
        0, 0, 4,
            -3,  3, -1,
            4,  2, -1,
            6,  3, -1,

            9, -1,  4,
        // 4. StateName
        0, 0, 5,
            -3,  3, -1,
            4,  2, -1,
            6,  3, -1,

            8,  6,  5,
            10, -1, 12,
        // 5. StateHeartBeatInterval
        0, 0, 6,
            -3,  3, -1,
            4,  2, -1,
            6,  3, -1,

            7,  7,  6,
            9,  7, -1,
            9,  8,  8,
        // 6. StateHeartBeats
        0, 0, 5,
            -3,  3, -1,
            4,  2, -1,
            6,  3, -1,

            7,  8,  7,
            9,  8,  8,
        // 7. AtStateEntry
        0, 0, 4,
            -3,  3, -1,
            4,  2, -1,
            6,  3, -1,

            9, -1,  8,
        // 8. Event
        0, 0, 6,
            -3,  3, -1,
            4,  2, -1,
            6,  3, -1,

            8,  9, -1,
            8, 10,  9,
            10, -1,  4,
        // 9. Action
        0, 0, 4,
            -3,  3, -1,
            4,  2, -1,
            6,  3, -1,

            8, 11, 10,
        // 10. NextState
        0, 0, 4,
            -3,  3, -1,
            4,  2, -1,
            6,  3, -1,

            8, 12, 11,
        // 11. AtStateExit
        0, 0, 6,
            -3,  3, -1,
            4,  2, -1,
            6,  3, -1,

            10, 13,  4,
            8,  9, -1,
            8, 10,  9,
        // 12. AtSmExit
        0, 0, 2,
            -3,  16, -1,
            11,  14, 13,
        // 13. Exit
        0, 0, 1,
            -3, -2, -1
};
#endif

// -------------------------------------------------------------------------------------------------

void test_performance( )
{
    OsTime t;
    const int n = 100;

#if defined( SEA_OS_LINUX )
    const char* file = "../tests/test_parser.txt";
#else
    const char* file = "tests\\test_parser.txt";
#endif

    int a;
    int b;
    int c;
    {
        vector< TestParser* > storage;
        t.start( );
        for( int i = 0; i < n; i++ )
        {
            storage.insert( storage.end( ), 0 );
        }
        a = t.elapsed( );
        SEA_TRACE( "** Overhead time: %f ms", ( double )( a ) / n );

        for( int i = 0; i < n; i++ )delete storage[ i ];
    }
    Engine::terminate( );

    {
        vector< TestParser* > storage;
        t.start( );
        for( int i = 0; i < n; i++ )
        {
            TestParser* obj = NEW TestParser;
            storage.insert( storage.end( ), obj );
        }
        b = t.elapsed( );
        for( int i = 0; i < n; i++ )delete storage[ i ];

        SEA_TRACE( "** Instantiate state machine time: %f ms", ( double )( b ) / n );

    }
    Engine::terminate( );

    {
        #include "../src/sea_parser.hxx"

        vector< TestParser* > storage;
        t.start( );
        for( int i = 0; i < n; i++ )
        {
            TestParser* obj = NEW TestParser;
            storage.insert( storage.end( ), obj );
            obj->create( i, the_source );
        }
        c = t.elapsed( );
        for( int i = 0; i < n; i++ )delete storage[ i ];

        SEA_TRACE( "** STRING Source code compile time: %f ms", ( double )( c ) / n );
    }
    Engine::terminate( );

    {
        vector< TestParser* > storage;
        t.start( );
        for( int i = 0; i < n; i++ )
        {
            TestParser* obj = NEW TestParser;
            storage.insert( storage.end( ), obj );
            obj->create( i, file, StateMachine::eFile );
        }
        c = t.elapsed( );
        for( int i = 0; i < n; i++ )delete storage[ i ];

        SEA_TRACE( "** FILE Source code compile time: %f ms", ( double )( c ) / n );
    }
    Engine::terminate( );

    {
        vector< TestParser* > storage;
        t.start( );
        for( int i = 0; i < n; i++ )
        {
            TestParser* obj = NEW TestParser;
            storage.insert( storage.end( ), obj );
            obj->create( i, the_bin );
        }
        c = t.elapsed( );
        for( int i = 0; i < n; i++ )delete storage[ i ];

        SEA_TRACE( "** BIN code compile time: %f ms", ( double )( c ) / n );
    }
    Engine::terminate( );
}

// -------------------------------------------------------------------------------------------------
